<?php
namespace app\admin\controller;

use app\admin\module\AdminModule;
use app\BaseController;
use app\common\validate\Admin as AdminValidate;
use app\Request;
use think\Exception;

class Admin extends BaseController{

    public function index(Request $request){
        $data = AdminModule::page($request->uid,$request->param("pageSize",10));
        return $this->success("获取成功",$data);
    }

    public function create(Request $request){
        $data = $request->post();
        $validate = new AdminValidate();
        if(!$validate->scene('add')->check($data)){
            throw new Exception($validate->getError());
        }
        AdminModule::create($data);
        return $this->success("添加成功");
    }

    public function update(Request $request){
        $data = $request->post();
        $validate = new AdminValidate();
        if(!$validate->scene('edit')->check($data)){
            throw new Exception($validate->getError());
        }
        AdminModule::update($data);
        return $this->success("更新成功");
    }

    public function remove(Request $request){
        AdminModule::del($request->param('id'));
        return $this->success("删除成功");
    }

    public function role(Request $request){
        $post = $request->post();
        if(empty($post['id'])){
            throw new Exception("请选择用户");
        }
        if(empty($post['role_id'])){
            throw new Exception('请选择角色');
        }
        AdminModule::saveRole($post['id'],$post['role_id']);
        return $this->success("操作成功");
    }

}